<template>
  <div class="com_box" ref="sonref">
    <el-button type="primary" class="button" @click="click">{{
      isFullscreen ? "退出全屏" : "全屏按钮"
    }}</el-button>
  </div>
</template>

<script>
import screenfull from "screenfull";

export default {
  name: "ScreenfullCom",
  data() {
    return {
      isFullscreen: false,
    };
  },

  mounted() {
    this.init();
  },
  beforeDestroy() {
    this.destroy();
  },
  methods: {
    click() {
      if (!screenfull.isEnabled) {
        this.$message({ message: "你的浏览器不支持全屏", type: "warning" });
        return false;
      }
      screenfull.toggle();
    },
    change() {
      this.isFullscreen = screenfull.isFullscreen;
    },
    init() {
      if (screenfull.isEnabled) {
        // 注册监听器
        screenfull.on("change", this.change);
      }
    },
    destroy() {
      if (screenfull.isEnabled) {
        // 注销监听器
        screenfull.off("change", this.change);
      }
    },
  },
};
</script>

<style scoped lang="scss"></style>
